<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *房屋管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'房屋列表'),
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['room_num'])){
		$params['room_num like']='%'.trim($_RQ['room_num']).'%';
		$return['params']['room_num']=$_RQ['room_num'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','bid','dongshu','danyuan','floor','room_num','room_space','room_type','status');
	$list=pdo_getslice('jy_smallquan_house',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$building=pdo_get('jy_smallquan_building',array('id'=>$item['bid']));
			$item['building']=$building;
			$item['dongshu']=$item['dongshu'].'栋';
			$item['danyuan']=$item['danyuan'].'单元';
			if(!$item['status']){
				$status=pdo_getcolumn('jy_smallquan_household',array('shenfen'=>1,'hid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'status');
				if($status){
					pdo_update('jy_smallquan_house',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
					$item['status']=1;
				}
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'building.area','title'=>'区域','type'=>'string'),
			array('name'=>'building.qishu','title'=>'期数','type'=>'string'),
			array('name'=>'dongshu','title'=>'栋数','type'=>'string'),
			array('name'=>'danyuan','title'=>'单元','type'=>'string'),
			array('name'=>'room_num','title'=>'房号','type'=>'string'),
			array('name'=>'room_type','title'=>'房型','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)),
		'search'=>array(
			array('name'=>'room_num','width'=>15,'title'=>'房号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status_select'),
		)
	);
	$return['status']=array('闲置','已入住');
	$return['status_select']=array(
		array('id'=>'0','title'=>'闲置'),
		array('id'=>'1','title'=>'已入住'),	
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_house',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回房屋列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'area','title'=>'区域','type'=>'string'),
					array('name'=>'qishu','title'=>'期数','type'=>'string'),
					array('name'=>'dongshu','title'=>'栋数','type'=>'string'),
					array('name'=>'danyuan','title'=>'单元','type'=>'string'),
					array('name'=>'room_num','title'=>'房号','type'=>'string'),
					array('name'=>'room_type','title'=>'房型','type'=>'string'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_smallquan_house',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}